﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Examination.BusinessLogicLayer;

public partial class Web_DiffManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string loginName = Session["userID"].ToString();
            Users user = new Users();
            user.LoadData(loginName);
            labUser.Text = user.UserName;
            InitData();  //初始化试卷科目          
        }
    }
    //初始化试卷科目
    protected void InitData()
    {
        Diff Diff = new Diff();       //创建试卷科目对象
        DataSet ds = Diff.QueryDiff();  //查询试卷科目信息
        GridView1.AllowPaging = true;//是否启用分页
        GridView1.PageSize = 8;//每页显示条数
        GridView1.DataSource = ds;          //为GridView控件指名数据源        
        GridView1.DataBind();               //绑定数据

        //如果当前索引为0 则 禁用 首页 上一页 按钮
        if (GridView1.PageIndex == 0)
        {
            this.LinkButton1.Enabled = false;
            this.LinkButton2.Enabled = false;
        }
        else
        {
            //否则开启按钮
            this.LinkButton1.Enabled = true;
            this.LinkButton2.Enabled = true;
        }
        //如果当前索引为-1 则 禁用 下一页 尾页 按钮 按钮
        if (GridView1.PageIndex == GridView1.PageCount - 1)
        {
            this.LinkButton3.Enabled = false;
            this.LinkButton4.Enabled = false;
        }
        else
        {
            //否则开启按钮
            this.LinkButton3.Enabled = true;
            this.LinkButton4.Enabled = true;
        }


    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        InitData();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int i;
        //执行循环，保证每条数据都可以更新
        for (i = 0; i < GridView1.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E4ECFB'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
    }
    //删除试卷科目事件
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Diff Diff = new Diff();           //创建Diff对象
        int ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString()); //取出要删除记录的主键值
        if (Diff.DeleteByProc(ID))
        {
            Response.Write("<script language=javascript>alert('成功删除试卷科目！')</script>");
        }
        else
        {
            Response.Write("<script language=javascript>alert('删除试卷科目失败！')</script>");
        }
        GridView1.EditIndex = -1;
        InitData();
    }
    //GridView控件RowUpdating事件
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString()); //取出要删除记录的主键值
        Diff Diff = new Diff();           //创建Diff对象
        Diff.DiffName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName")).Text;
        if (Diff.UpdateByProc(ID))//使用Users类UpdateByProc方法修改用户信息
        {
            Response.Write("<script language=javascript>alert('修改成功!')</script>");
        }
        else
        {
            Response.Write("<script language=javascript>alert('修改失败!')</script>");
        }
        GridView1.EditIndex = -1;
        InitData();
    }
    //GridView控件RowCanceling事件
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        InitData();
    }
    //GridView控件RowEditing事件
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;  //GridView编辑项索引等于单击行的索引
        InitData();
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        switch (((LinkButton)sender).CommandArgument.ToString())
        {
            //将不同CommandArgument参数  而同一CommandName名字的4个按钮 循环取CommandArgument参数 利用索引进行判断
            //从而实现分页功能 注意的一点是 4个 按钮必须要触发同一个  单击事件
            case "first":
                GridView1.PageIndex = 0;
                break;
            case "last":
                GridView1.PageIndex = GridView1.PageCount - 1;
                break;
            case "prev":
                GridView1.PageIndex = GridView1.PageIndex - 1;
                break;
            case "next":
                GridView1.PageIndex = GridView1.PageIndex + 1;
                break;

        }
        //之后 重新绑定
        InitData();
    }

}
